package com.maple.common.utils

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale

/**
 * Tip:
 * Create by SeVen on 2023/7/25 18:17
 */
object DateUtils {
    /**
     * data to String
     */
    @JvmStatic
    @Throws(ParseException::class)
    fun dateToString(data: Date, formatType: String): String {
        return SimpleDateFormat(formatType, Locale.CHINA).format(data)
    }

    /**
     * long To String
     */
    @JvmStatic
    @Throws(ParseException::class)
    fun longToString(currentTime: Long, formatType: String): String? {
        val date: Date? = longToDate(currentTime, formatType) // long类型转成Date类型
        return date?.let { dateToString(it, formatType) }
    }

    /**
     * String to Date
     */
    @JvmStatic
    @Throws(ParseException::class)
    fun stringToDate(strTime: String, formatType: String): Date? {
        val sdf = SimpleDateFormat(formatType, Locale.CHINA)
        return sdf.parse(strTime)
    }

    /**
     * long to Date
     */
    @JvmStatic
    @Throws(ParseException::class)
    fun longToDate(currentTime: Long, formatType: String): Date? {
        val dateOld = Date(currentTime) // 根据long类型的毫秒数生命一个date类型的时间
        val sDateTime: String = dateToString(dateOld, formatType) // 把date类型的时间转换为string
        return stringToDate(sDateTime, formatType)
    }

    /**
     * String to Long
     */
    @JvmStatic
    @Throws(ParseException::class)
    fun stringToLong(strTime: String, formatType: String): Long? {
        val date = stringToDate(strTime, formatType) // String类型转成date类型
        return date?.let { dateToLong(it) }
    }

    /**
     * string To String
     */
    @JvmStatic
    @Throws(ParseException::class)
    fun stringToString(strTime: String, orFormatType: String, formatType: String): String? {
        val currentTime = stringToLong(strTime, orFormatType)
        return currentTime?.let { longToString(it, formatType) }
    }

    /**
     * Date to Long
     */
    @JvmStatic
    @Throws(ParseException::class)
    fun dateToLong(date: Date): Long {
        return date.time
    }

    /**
     * 今天
     */
    fun isToDay(currentTime: Long): Boolean {
        val timeInfo = getToday()
        return currentTime > timeInfo.statTime && currentTime < timeInfo.endTime
    }

    /**
     * 获取当前年份
     */
    @JvmStatic
    fun getYear(): Int {
        val calendar = Calendar.getInstance()
        return calendar[Calendar.YEAR]
    }

    /**
     * 获取月
     * @return
     */
    @JvmStatic
    fun getMonth(): Int {
        val cd = Calendar.getInstance()
        return cd[Calendar.MONTH] + 1
    }

    @JvmStatic
    fun getYearUpperOrDown(): String {
        return if (getMonth() < 7) {
            "上半年"
        } else {
            "下半年"
        }
    }

    private fun getToday(): TimeInfo {
        val startCalendar = Calendar.getInstance()
        startCalendar[11] = 0
        startCalendar[12] = 0
        startCalendar[13] = 0
        startCalendar[14] = 0
        val endCalendar = Calendar.getInstance()
        endCalendar[11] = 23
        endCalendar[12] = 59
        endCalendar[13] = 59
        endCalendar[14] = 999
        return TimeInfo(startCalendar.time.time, endCalendar.time.time)
    }

    /**
     * 昨天
     */
    fun isYesterday(currentTime: Long): Boolean {
        val timeInfo = getToday()
        return currentTime > timeInfo.statTime && currentTime < timeInfo.endTime
    }

    private fun isYesterday(): TimeInfo {
        val startCalendar = Calendar.getInstance()
        startCalendar.add(5, -1)
        startCalendar[11] = 0
        startCalendar[12] = 0
        startCalendar[13] = 0
        startCalendar[14] = 0
        val endCalendar = Calendar.getInstance()
        endCalendar.add(5, -1)
        endCalendar[11] = 23
        endCalendar[12] = 59
        endCalendar[13] = 59
        endCalendar[14] = 999
        return TimeInfo(startCalendar.time.time, endCalendar.time.time)
    }

    /**
     * 是否为同一年
     */
    fun isSameYear(currentTime: Long): Boolean {
        val default = Calendar.getInstance(Locale.CHINA)
        default.timeInMillis = System.currentTimeMillis()
        val currentYear = default.get(Calendar.YEAR)
        val calendar = Calendar.getInstance(Locale.CHINA)
        calendar.timeInMillis = currentTime
        val year = calendar.get(Calendar.YEAR)
        return currentYear == year
    }
}

data class TimeInfo(val statTime: Long, val endTime: Long)
